import { Component, OnInit, Output, EventEmitter, Inject, forwardRef, ChangeDetectorRef } from '@angular/core';
import { path } from '../../../../../path.config';
import { masterService } from '../../../../../../lib/master.service';
import { AppComponent } from '../../../../../app.component';
import { NzModalService } from 'ng-zorro-antd';

@Component({
    selector: 'voucher-batch-item',
    templateUrl: './result.component.html',
    styleUrls: ['./result.component.css'],
    providers: [forwardRef(() => AppComponent)]
})
export class ResultComponent implements OnInit {
    @Output() closeModal = new EventEmitter<any>();

    // 列表API
    public getItemList: any = path.voucherManage.voucherBatch.getItemList;

    private data: any;
    public rows: Array<any> = [];
    public item = {
        rowId: 0
    };

    // 表格数据头部标题
    public columns: Array<any> = [
        { title: '账号', name: 'account' },
        { title: '券编号', name: 'voucherSn' },
        { title: '状态', name: 'status' },
        { title: '备注', name: 'description' },
    ];
    // 当前页码
    public page: any = 1;
    // 一共多少页
    public itemsPerPage: number = 10;
    // 分页最多显示的页数
    public maxSize: number = 5;
    // 总页数计数变化时，事件数等于总页数计数
    public numPages: number = 1;
    // 总共的列表项数
    public totalItems: number = 0;
    // 分页器的文本配置
    public localText = {
        firstBtn: '首页',
        lastBtn: '末页',
        previousBtn: '上一页',
        nextBtn: '下一页'
    }

    public filter: any = {
        id: this.item.rowId,
        pageSize: '10',
        pageIndex: '1'
    }
    constructor(public _masterService: masterService,
        public cdRef: ChangeDetectorRef,
        private confirmServ: NzModalService,
        @Inject(forwardRef(() => AppComponent)) private app: AppComponent) { }

    public ngOnInit() {

    }

    // 渲染表格
    public onChangeTable = (urlObj?, id?) => {
        if (urlObj) {
            this.filter.pageSize = urlObj.itemsPerPage;
            this.filter.pageIndex = urlObj.page;
        }
        if (id) {
            this.item.rowId = id;
            this.filter.id = id;
        }
        this._masterService
            .getData(this.getItemList, this.filter)
            .subscribe((res) => {
                if (res.statusCode == 200) {
                    this.rows = res.data.items;
                    this.totalItems = res.data.pagination.totalCount;
                    this.itemsPerPage = res.data.pagination.pageSize;
                    this.page = parseInt(res.data.pagination.pageIndex);
                    this.cdRef.detectChanges();
                } else if (res.statusCode == 401) {
                    this.app.logOff();
                } else {
                    this.app.toastError('错误信息： ' + res.errorMessage);
                }
            })
    }
}
